#!/usr/bin/env python
# -*- coding:utf-8 -*-
import paramiko
import uuid
#两个方法都用一个连接，减少连接开销

class Haproxy(object):
    def __init__(self):
        self.host='192.168.136.8'
        self.port=22
        self.username='root'
        self.pwd='123456'
    def create_file(self):
        file_name=str(uuid.uuid4())
        #uuid('9fb01137-e13d-4ea8-b16e-d7861c2b8d9a')==>'9fb01137-e13d-4ea8-b16e-d7861c2b8d9a'
        with open(file_name,'wb')as f:
            f.write('test')
        return file_name

    def run(self):
        self.connect()
        self.upload()
        self.rename()
        self.close()

    def connect(self):
        transport=paramiko.Transport((self.host,self.port))
        transport.connect(username=self.username,password=self.pwd)
        self.__transport=transport

    def close(self):
        self.__transport.close()

    def upload(self):
        file_name=self.create_file()
        sftp=paramiko.SFTPClient.from_transport(self.__transport)
        sftp.put(file_name,'/tmp/ttt.py')

    def rename(self):
        ssh = paramiko.SSHClient()
        ssh._transport=self.__transport
        stdin,stdout,stderro=ssh.exec_command('mv /tmp/ttt.py /tmp/nnn.py')
        result=stdout.read()
        print result

obj=Haproxy()
obj.run()